#-*- coding:utf-8 -*-
from multiprocessing import Process,Value
import time
import ippool
import redis
host = 'localhost'
port = '6379'
db = 0
r = redis.Redis(host=host, port=port, db=db)

def testProxyRun(goflag):
    while True:
        proxy = r.rpop('proxys')
        if proxy == None:
            if goflag.value == 1:
                break
            time.sleep(20)
        else:
            ippool.testWriteProxy(proxy)
    print 'testProxyRun is stop!'

def addOriginProxy(goflag):
    pagecode = ippool.downloadPage("http://www.xicidaili.com/nn/1")
    ippool.processXiciPage(pagecode)
    goflag.value = 1
    print 'addOriginProxy'
if __name__ == "__main__":
    goflag = Value('i',0)
    aor = Process(target=addOriginProxy, name="addOriginProxy", args=(goflag,))
    aor.start()
    aor.join()
    
    tpr = Process(target=testProxyRun, name="testProxyRun", args=(goflag,))
    tpr.start()
    tpr.join()
    print 'Main is success!'
    
    